package StatuSync::Log;
use strict;

use POSIX;
use Data::Dumper;

sub log {
	warn shift->msg(@_);
}

sub die {
	die shift->msg(@_);
}

sub msg {
	my $self = shift;
	my $level = shift;
	return sprintf "%s [%s] %s %s\n",
		strftime("%Y-%m-%d %H:%M:%S", localtime),
		$level, $self->caller,
		join(' ',map { ref $_ ? $self->dump($_,2) : $_ } @_);
}

sub dump {
	my $self = shift;
	my $subject = shift;
	my $depth = shift || 5;
	my $dump = Data::Dumper->new([$subject])->Indent(1)->Quotekeys(0)->Maxdepth($depth)->Sortkeys(1)->Dump;
	$dump =~ s/^\$VAR1 = //;
	$dump =~ s/\s+/ /g if length $dump < 100;
	return $dump;
}

sub caller {
	my $self = shift;
	(caller($_))[0] ne __PACKAGE__ && return (caller($_+1))[3] for 0 .. 100;
}

sub debug { shift->log('debug', @_) }
sub info { shift->log('info', @_) }
sub warning { shift->log('warning', @_) }
sub error { shift->log('error', @_) }

1;

